package com.youkeyi.ddy.cloud.application.system.tag.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.tag.SystemTagCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.tag.dto.SystemTagListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.tag.dto.SystemTagListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.tag.dto.SystemTagQueryBindListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.tag.dto.SystemTagQueryBindListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.tag.param.*;
import com.youkeyi.ddy.cloud.common.api.basic.pub.tag.PublicTagCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.tag.dto.PublicTagListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.tag.dto.PublicTagQueryBindListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.tag.param.*;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * SystemTagController
 *
 * @Author liuxiawang
 * @Date 2024/3/7 3:05 PM
 * @Location shenzhen.china
 */
@Service
public class SystemTagController implements SystemTagCloudService {

    @Autowired
    private PublicTagCloudService publicTagCloudService;

    @Override
    public SystemTagListCloudDto list(SystemTagListCloudParam param) {
        SystemTagListCloudDto dto = new SystemTagListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicTagQueryCloudParam tagQueryCloudParam = new PublicTagQueryCloudParam();
        tagQueryCloudParam.setTagPrimaryKeys(param.getTagPrimaryKeys());
        tagQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        tagQueryCloudParam.setCodes(param.getCodes());
        tagQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        tagQueryCloudParam.setParentKeys(param.getParentKeys());
        tagQueryCloudParam.setPageSize(param.getPageSize());
        tagQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicTagListCloudDto tagDtos = publicTagCloudService.query(tagQueryCloudParam);
        if (null != tagDtos && CollectionUtils.isNotEmpty(tagDtos.getRows())) {
            List<SystemTagListItemCloudDto> items = new ArrayList<>();
            tagDtos.getRows().forEach(tag -> {
                SystemTagListItemCloudDto item = new SystemTagListItemCloudDto();
                item.setTagPrimaryKey(tag.getTagPrimaryKey());
                item.setCorpKey(tag.getCorpKey());
                item.setName(tag.getName());
                item.setCode(tag.getCode());
                item.setIcon(tag.getIcon());
                item.setIconColor(tag.getIconColor());
                item.setParentKey(tag.getParentKey());
                item.setDesc(tag.getDesc());
                item.setRemark(tag.getRemark());
                item.setIsDeleted(tag.getIsDeleted());
                item.setIsDeletedName(tag.getIsDeletedName());
                item.setCreateUserKey(tag.getCreateUserKey());
                item.setCreateTime(tag.getCreateTime());
                item.setUpdateUserKey(tag.getUpdateUserKey());
                item.setUpdateTime(tag.getUpdateTime());
                item.setCreateUserName(tag.getCreateUserName());
                item.setUpdateUserName(tag.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(tagDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(SystemTagCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicTagCreateCloudParam tagCreateCloudParam = new PublicTagCreateCloudParam();
        tagCreateCloudParam.setCorpKey(corpKey);
        tagCreateCloudParam.setName(param.getName());
        tagCreateCloudParam.setCode(param.getCode());
        tagCreateCloudParam.setIcon(param.getIcon());
        tagCreateCloudParam.setIconColor(param.getIconColor());
        tagCreateCloudParam.setParentKey(param.getParentKey());
        tagCreateCloudParam.setDesc(param.getDesc());
        tagCreateCloudParam.setRemark(param.getRemark());
        tagCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        tagCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicTagCloudService.create(tagCreateCloudParam);
    }

    @Override
    public ResponseContent update(SystemTagUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getTagPrimaryKey())
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getCode())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicTagUpdateCloudParam tagUpdateCloudParam = new PublicTagUpdateCloudParam();
        tagUpdateCloudParam.setTagPrimaryKey(param.getTagPrimaryKey());
        tagUpdateCloudParam.setCorpKey(corpKey);
        tagUpdateCloudParam.setName(param.getName());
        tagUpdateCloudParam.setCode(param.getCode());
        tagUpdateCloudParam.setIcon(param.getIcon());
        tagUpdateCloudParam.setIconColor(param.getIconColor());
        tagUpdateCloudParam.setDesc(param.getDesc());
        tagUpdateCloudParam.setRemark(param.getRemark());
        tagUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        tagUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicTagCloudService.update(tagUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(SystemTagDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getTagPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicTagDeleteCloudParam tagDeleteCloudParam = new PublicTagDeleteCloudParam();
        tagDeleteCloudParam.setTagPrimaryKeys(param.getTagPrimaryKeys());
        tagDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        tagDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicTagCloudService.delete(tagDeleteCloudParam);
    }








    @Override
    public ResponseContent bind(SystemTagBindCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getTagPrimaryKeys()) || StringUtils.isBlank(param.getTargetPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        List<PublicTagBindItemCloudParam> bindItems = new ArrayList<>();
        param.getTagPrimaryKeys().forEach(tagKey -> {
            PublicTagBindItemCloudParam item = new PublicTagBindItemCloudParam();
            item.setTagPrimaryKey(tagKey);
            item.setTargetPrimaryKey(param.getTargetPrimaryKey());
            bindItems.add(item);
        });
        PublicTagBindCloudParam tagBindCloudParam = new PublicTagBindCloudParam();
        tagBindCloudParam.setBindItems(bindItems);
        tagBindCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        tagBindCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicTagCloudService.bind(tagBindCloudParam);
    }

    @Override
    public ResponseContent unbind(SystemTagUnbindCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getTagPrimaryKeys()) || StringUtils.isBlank(param.getTargetPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        List<PublicTagUnbindItemCloudParam> unbindItems = new ArrayList<>();
        param.getTagPrimaryKeys().forEach(tagKey -> {
            PublicTagUnbindItemCloudParam item = new PublicTagUnbindItemCloudParam();
            item.setTagPrimaryKey(tagKey);
            item.setTargetPrimaryKey(param.getTargetPrimaryKey());
            unbindItems.add(item);
        });
        PublicTagUnbindCloudParam tagUnBindCloudParam = new PublicTagUnbindCloudParam();
        tagUnBindCloudParam.setUnbindItems(unbindItems);
        return publicTagCloudService.unbind(tagUnBindCloudParam);
    }

    @Override
    public SystemTagQueryBindListCloudDto queryBind(SystemTagQueryBindCloudParam param) {
        SystemTagQueryBindListCloudDto dto = new SystemTagQueryBindListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicTagQueryBindCloudParam tagQueryBindCloudParam = new PublicTagQueryBindCloudParam();
        tagQueryBindCloudParam.setTargetPrimaryKeys(param.getTargetPrimaryKeys());
        PublicTagQueryBindListCloudDto tagDtos = publicTagCloudService.queryBind(tagQueryBindCloudParam);
        if (null != tagDtos && CollectionUtils.isNotEmpty(tagDtos.getRows())) {
            List<SystemTagQueryBindListItemCloudDto> items = new ArrayList<>();
            tagDtos.getRows().forEach(tag -> {
                SystemTagQueryBindListItemCloudDto item = new SystemTagQueryBindListItemCloudDto();
                item.setTagPrimaryKey(tag.getTagPrimaryKey());
                item.setTargetPrimaryKey(tag.getTargetPrimaryKey());
                item.setCorpKey(tag.getCorpKey());
                item.setName(tag.getName());
                item.setCode(tag.getCode());
                item.setIcon(tag.getIcon());
                item.setCreateUserKey(tag.getCreateUserKey());
                item.setCreateUserName(tag.getCreateUserName());
                item.setCreateTime(tag.getCreateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(tagDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }
}
